<template>
  <div class="container">
    <!--  主题内容标题   -->
    <div class="flex contain-header">
      <div class="contain-title">账号管理</div>
    </div>
    <div class="user-avatar"><img :src="'http://zctuchuang.oss-cn-shenzhen.aliyuncs.com/'+ avatar"></div>
    <div class="reset-username" >
      <div class="user-name">{{ userName }}</div>
<!--      <a-input class="user-name-input" v-model="userName" v-else/>-->
<!--      <div class="icon-contain" @click="handleEdit">-->
<!--        <icon-svg name="iconbianji" class="resetusername-icon"/>-->
<!--      </div>-->
    </div>
    <a-form :form="form" @submit="submitInfo" :label-col="{ span: 4,offset: 1 }" :wrapper-col="{ span: 15,offset: 1 }">
      <a-form-item label="真实姓名">
        <a-input placeholder="请输入真实姓名" allowClear  v-decorator="['userRealname', { rules: [{ required: true, message: '请输入真实姓名!' }],initialValue: data.userRealname }]"/>
      </a-form-item>
      <a-form-item label="身份">
        <a-radio-group name="radioGroup" v-decorator="['userRole',{initialValue: data.userRole}]">
          <a-radio :value="0">
            学生
          </a-radio>
          <a-radio :value="1">
            老师
          </a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="性别">
        <a-radio-group name="radioGroup2" v-decorator="['userSex',{initialValue: data.userSex}]">
          <a-radio :value="1">
            男
          </a-radio>
          <a-radio :value="0">
            女
          </a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="学校">
        <a-input placeholder="请输入学校" allowClear  v-decorator="['stSchool', { rules: [{ required: true, message: '请输入学校!' }],initialValue: data.stSchool }]"/>
      </a-form-item>
      <a-form-item label="学院">
        <a-input placeholder="请输入学院" allowClear  v-decorator="['stCollege', { rules: [{ required: true, message: '请输入学院!' }],initialValue: data.stCollege }]"/>
      </a-form-item>
      <template v-if="authority === 1">
        <a-form-item label="班级">
          <a-input placeholder="请输入班级" allowClear  v-decorator="['stClass', { rules: [{ required: true, message: '请输入班级!' }],initialValue: data.stClass }]"/>
        </a-form-item>
        <a-form-item label="学号">
          <a-input placeholder="请输入学号" allowClear  v-decorator="['stNo', { rules: [{ required: true, message: '请输入学号!' }],initialValue: data.stNo }]"/>
        </a-form-item>
      </template>
      <a-row class="flex row btn-margin">
        <div>
          <template>
            <a-button type="primary" html-type="submit">提交</a-button>
          </template>
          <a-button class="btn-space" @click="handleReset">重置</a-button>
        </div>
      </a-row>
    </a-form>
  </div>
</template>

<script>

import {getUserInfo, editUserInfo} from "@/api";

  export default {
    name: "AccountManagement.vue",
    data() {
      return {
        form: this.$form.createForm(this, { name: 'coordinated' }),
        authority: 1,
        isEdit: false,
        userName: window.localStorage.getItem('userName'),
        data: {},
        avatar: window.localStorage.getItem('avatar')
      }
    },
    created() {
      this.getInfo()
    },
    methods: {
      //获取用户信息
      getInfo() {
        let _this = this
        let params = {
          userId: window.localStorage.getItem('userId')
        }
        getUserInfo(params)
          .then((res) => {
            if (res.status == 200) {
              _this.data = res.data
            } else {
              this.$message.info(res.msg);
            }
          })
          .catch((err) => {
            this.$message.error(err);
          })
      },
      //修改个人信息
      submitInfo(e) {
        e.preventDefault();
        let data
        console.log('t提交')
        this.form.validateFields((err, values) => {
          if (!err) {
            data = this.form.getFieldsValue()
            data.userId = window.localStorage.getItem('userId')
            console.log('提交', data)
            if(data) {
              editUserInfo(data)
                .then((res) => {
                  if (res.status == 200){
                    this.$message.success('修改用户信息成功！');
                    this.getInfo()
                  }else{
                    this.$message.info(res.msg);
                    this.handleReset()
                  }
                })
                .catch((err) => {
                  this.$message.error(err);
                  this.handleReset()
                })
            }
          }
        });
      },
      handleReset() {
        this.form.resetFields();
      },
      // //编辑修改昵称
      // handleEdit() {
      //   this.isEdit = !this.isEdit
      // }
    }
  }
</script>

<style scoped>
  .flex{
    display: flex;
    flex-direction: row;
    /* border:1px solid red; */
  }
  .user-name-input {
    width: 120px;
  }
  .container{
    width: 1032px;
    height: 996px;
    background-color: #ffffff;
    border-radius: 4px;
    border: 1px solid #eeeeee;
    padding: 5px 13px;
    /* border:1px solid orange; */
  }
  .contain-header{
    justify-content: space-between;
    align-items: center;
    /* border:1px solid green; */
  }
  .contain-title{
    font-family: MicrosoftYaHei;
    font-size: 19px;
    font-weight: bold;
    font-stretch: normal;
    letter-spacing: 1px;
    color: #1bcbb4;
    margin-top: 10px;
    /* border:1px solid pink; */
  }
  .user-avatar{
    text-align:center;
  }
  .user-avatar img{
    width: 120px;
    height: 120px;
    border-radius:100px;
    /* border:1px solid black; */
  }

  .user-name{

    /* margin-top: 10px; */
    /* text-align:center;
    position: absolute;
    left: 50%;
    transform: translate(-50%); */

    font-size: 20px;

    /* display: inline-block; */
  }
  .icon-contain{
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-content: center;
    align-items: center;
    cursor: pointer;
    /* background-color: yellow; */
  }
  .resetusername-icon{
    font-size: 8px;
    margin-left: 5px;

    /* display: inline-block; */
  }
  .reset-username{
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    align-content: center;
  }
  .base-information{
    margin-bottom: 18px;
    margin-left: 10px;
    display: block;
    font-family: MicrosoftYaHei;
    font-size: 19px;
    font-weight: bold;
    font-stretch: normal;
    line-height: 40px;
    letter-spacing: 1px;
    color: #333333;
  }
  .sex{
    color: #000;
    font-size: 14px;
    margin-left: 10px;
    margin-right: 20px;
  }
  .red-point{
    margin-left: 20px;
    margin-bottom: 3px;
    width: 6px;
    height: 6px;
    border-radius: 10px;
    display: inline-block;
    background-color: red;
  }
  .row{
    display: flex;
    flex-direction: row;
    justify-items: center;
    justify-content: center;
    margin-bottom: 15px;
  }
  .btn-space{
    margin-left: 80px;
  }

  .name-course{
    margin-top: 20px;
    width: 100%;
    height: 300px;
    /* border:1px solid red; */
  }
  .submit-button{
    /* border:1px solid red; */
    width: fit-content;
    margin:100px auto;
  }

</style>